package hu.jonas.allunit.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;

public class ProgressPanel extends Composite {

	private Label runStatus;
	private Label errorStatus;
	private Label failureStatus;
	private ProgressBar progressBar;
	
	private Color red;
	private Color green;

	public ProgressPanel(Composite parent, int style) {
		super(parent, style);
		
		red = new Color(parent.getDisplay(), 159, 63, 63);
		green = new Color(parent.getDisplay(), 95, 191, 95);
		
		GridLayout layout = new GridLayout(3, true);
		layout.horizontalSpacing = 10;
		this.setLayout(layout);
		
		progressBar = new ProgressBar(this, SWT.HORIZONTAL | SWT.SMOOTH);
		GridData progressBarData = new GridData(GridData.FILL_HORIZONTAL);
		progressBarData.horizontalSpan = 3;
		progressBarData.heightHint = 16;
		progressBar.setLayoutData(progressBarData);
		progressBar.setForeground(green);
		
		runStatus = new Label(this, SWT.NONE);
		runStatus.setText("Completed:");
		runStatus.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING));
		
		errorStatus = new Label(this, SWT.NONE);
		errorStatus.setText("Errors: 0");
		errorStatus.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING));
		
		failureStatus = new Label(this, SWT.NONE);
		failureStatus.setText("Failures: 0");
		failureStatus.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING));
	}

	public void setMaximum(int maximum) {
		progressBar.setMaximum(maximum);
	}
	
	public void setState(int progress, boolean failed) {
		progressBar.setSelection(progress);
		progressBar.setForeground(failed ? red : green);
		runStatus.setText(String.format("Completed: %1$d", progress));
		redraw();
	}
	
}
